/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-27     yoyoyes      first version
 */
#include "thread_scheduler_hook_demo.h"

volatile rt_uint32_t count[2];

/*
 * @brief  线程入口函数
 *
 * @param  parameter 线程入口函数参数
 *
 * @return None
 */
static void thread_entry(void *parameter)
{
    rt_uint32_t value;

    value = (rt_uint32_t)parameter;

    while(1)
    {
        rt_kprintf("thread %d is running\n", value);
        rt_thread_mdelay(1000);
    }
}

static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;

/*
 * @brief  调度器钩子函数
 *
 * @param  from 线程控制块指针
 *
 * @param  to   线程控制块指针
 *
 * @return None
 */
static void hook_of_scheduler(struct rt_thread *from, struct rt_thread *to)
{
    rt_kprintf("from: %s -->  to: %s\n", from->name, to->name);
}

/*
 * @brief  线程调度器钩子Demo函数
 *
 * @param  None
 *
 * @return None
 */
void thread_scheduler_hook_demo(void)
{
    /* 设置调度器钩子 */
    rt_scheduler_sethook(hook_of_scheduler);

    /* 创建线程1 */
    tid1 = rt_thread_create("thread1", thread_entry, (void*)1, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if(tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }

    /* 创建线程2 */
    tid2 = rt_thread_create("thread2", thread_entry, (void*)2, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE - 5);
    if(tid2 != RT_NULL)
    {
        rt_thread_startup(tid2);
    }
}


